package com.caszx.chargingpile.activity;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONException;
import com.app.base.app.BaseTopBarDelayActivity;
import com.app.jsonparse.JSONParse;
import com.app.utils.DipPxUtil;
import com.app.utils.DiskUser;
import com.app.utils.RegularHelper;
import com.app.view.EditTextWithDel;
import com.app.view.ShapeTextView;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.entity.EntityStation;
import com.caszx.chargingpile.http.ConstantUrl;
import com.caszx.chargingpile.utils.DialogHelp;
import com.caszx.chargingpile.utils.SnackbarHelp;

import org.json.JSONObject;

import java.util.HashMap;

import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import okhttp3.Call;

/**
 * 修改密码
 * */
public class UserMdPassWdActivity extends BaseTopBarDelayActivity implements View.OnClickListener {

    private EditTextWithDel edit_old_pwd;
    private EditTextWithDel edit_new_pwd;
    private EditTextWithDel edit_sure_pwd;
    private ShapeTextView   save_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_md_pass_wd);
        setCenterTitle("修改密码");
    }

    @Override
    protected void findView() {
        edit_old_pwd = (EditTextWithDel) findViewById(R.id.edit_old_pwd);
        edit_new_pwd = (EditTextWithDel) findViewById(R.id.edit_new_pwd);
        edit_sure_pwd = (EditTextWithDel) findViewById(R.id.edit_sure_pwd);
        save_password = (ShapeTextView) findViewById(R.id.save_password);
        save_password.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String oldPwd = edit_old_pwd.getText().toString().trim();
        String newPwd = edit_new_pwd.getText().toString().trim();
        String confirmPwd = edit_sure_pwd.getText().toString().trim();

        if(TextUtils.isEmpty(oldPwd)){
            SnackbarHelp.showSnackbarMsg(UserMdPassWdActivity.this,getResources().getString(R.string.string_passwd_null));
            return;
        }
        if(!RegularHelper.checkPassword(newPwd,6,10)){
            SnackbarHelp.showSnackbarMsg(UserMdPassWdActivity.this,getResources().getString(R.string.regex_passwd_error));
            return;
        }

        if(!newPwd.equals(confirmPwd)){
            SnackbarHelp.showSnackbarMsg(UserMdPassWdActivity.this,getResources().getString(R.string.user_passwd_no_equals));
            return;
        }
        savaPassWd(oldPwd,newPwd,confirmPwd);
    }


    private void savaPassWd(String oldPwd,String newPwd,String confirmPwd){

        showProgressDialog("修改密码");
        String token = DiskUser.getStringValueByKey(this,"token");
        HashMap<String,String> params = new HashMap<>();
        params.put("confirmPwd",confirmPwd);
        params.put("oldPwd",oldPwd);
        params.put("pwd",newPwd);
        params.put("token",token);
        OkHttpUtils.post()
                .url(ConstantUrl.URL_PASSWD_MD)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        dissmissProgressDialog();
                         DialogHelp.showDialogOnlyMesg(UserMdPassWdActivity.this,getResources().getString(R.string.error_network),null);
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        dissmissProgressDialog();
                        JSONObject resultJsonObject =  response.optJSONObject("result");
                        if(resultJsonObject!=null){
                            int code =  resultJsonObject.optInt("code");
                            if(code<0){
                                //error
                                //密码修改失败
                                 DialogHelp.showDialogOnlyMesg(UserMdPassWdActivity.this,resultJsonObject.optString("msg"),null);
                            }else{
                                try {
                                    //密码修改成功
                                     DialogHelp.showDialogOnlyMesg(UserMdPassWdActivity.this,resultJsonObject.optString("msg"),null);
                                }catch (JSONException e){
                                    SnackbarHelp.showSnackbarMsg(UserMdPassWdActivity.this,getResources().getString(R.string.error_parser_json));
                                }
                            }
                        }
                    }
                });
    }
}
